package com.cuitrip.business.notice.ui;

import android.text.TextUtils;

import com.cuitrip.apiservice.MessageBusiness;
import com.cuitrip.app.base.CtApiCallback;
import com.cuitrip.app.base.CtException;
import com.cuitrip.app.base.ListFetchCallback;
import com.cuitrip.business.login.LoginInstance;
import com.cuitrip.business.notice.model.MessageServerItem;
import com.cuitrip.business.user.model.CtUserInfo;
import com.cuitrip.service.R;
import com.cuitrip.util.PlatformUtil;
import com.lab.network.config.CtResponseCode;
import com.lab.network.model.CtApiResponse;
import com.lab.network.model.IProxyCallback;
import com.lab.network.model.ProxyResult;
import com.lab.network.proxy.ApiProxy;
import com.lab.utils.LogHelper;
import com.lab.utils.MessageUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by baziii on 15/8/10.
 */
public class MessagePresent {
    public static final String TAG = "MessagePresent";


    IMessageListView mMessageView;
    IMessageFetcher mMessageFetcher = new MessageFetcher();
    int userType;

    public MessagePresent(IMessageListView mMessageView) {
        this.mMessageView = mMessageView;
        userType = LoginInstance.isLogin() ?
                0 : (LoginInstance.getInstance().getUserInfo().isTravel() ? CtUserInfo.USER_TRAVEL :
                CtUserInfo.USER_FINDER);
    }

    public void requestLoadMore() {
        LogHelper.e(TAG, "requestLoadMore");
        mMessageView.showLoadingMoreView();
        mMessageFetcher.getMessageListWithMore(mMessageView.getSize(), new ListFetchCallback<MessageMode>() {
            @Override
            public void onSuc(List<MessageMode> t) {
                LogHelper.e(TAG, "requestLoadMore onscu" + t.size());
                mMessageView.renderUIWithAppendData(t);
                mMessageView.hideLoadingMoreView();
            }

            @Override
            public void onFailed(Throwable throwable) {

                LogHelper.e(TAG, "requestLoadMore onfailed");
                MessageUtils.showToast(throwable.getMessage());
                mMessageView.hideLoadingMoreView();
            }
        });
    }

    public void onClickMessage(final MessageMode messageMode) {
        mMessageView.jumpMessage(messageMode);
    }

    public void onMove(final MessageMode messageMode) {
        mMessageFetcher.deleteMessage(messageMode.getId(), new CtApiCallback() {
            @Override
            public void onSuc() {
                mMessageView.deleteMessage(messageMode);
            }

            @Override
            public void onFailed(CtException throwable) {
                MessageUtils.showToast(throwable.getMessage());
            }
        });
    }

    public void requestRefresh() {
        LogHelper.e(TAG, "requestRefresh");
        mMessageFetcher.getMessageList(new ListFetchCallback<MessageMode>() {
            @Override
            public void onSuc(List<MessageMode> t) {
                LogHelper.e(TAG, "requestRefresh onsuc" + t.size());
                mMessageView.renderUIWithData(t);
                mMessageView.hideRefreshView();
                mMessageView.hideProgressView();
            }

            @Override
            public void onFailed(Throwable throwable) {
                LogHelper.e(TAG, "requestRefresh onfailed");
                MessageUtils.showToast(throwable.getMessage());
                mMessageView.hideRefreshView();
                mMessageView.hideProgressView();
            }
        });
    }

    public class MessageFetcher implements IMessageFetcher {

        int defaultsize = 20;

        @Override
        public void getMessageList(final ListFetchCallback<MessageMode> itemListFetchCallback) {
            MessageBusiness.getMessageList(new ApiProxy(new IProxyCallback() {
                @Override
                public boolean onProxyResult(ProxyResult<?> result) {
                    CtApiResponse response = (CtApiResponse) result.getData();
                    if (response != null && response.code == CtResponseCode.RESULT_OK) {
                        List<MessageMode> resultList = new ArrayList<>();
                        List<MessageServerItem> datas = (List<MessageServerItem>) response.result;
                        for (MessageServerItem item : datas) {
                            resultList.add(MessageMode.getInstance(item));
                        }
                        itemListFetchCallback.onSuc(resultList);
                        return true;
                    } else {
                        String msg;
                        if (response != null && !TextUtils.isEmpty(response.msg)) {
                            msg = response.msg;
                        } else {
                            msg = PlatformUtil.getInstance().getString(R.string.feedback_data_err);
                        }
                        itemListFetchCallback.onFailed(new CtException(msg));
                    }
                    return false;
                }
            }), userType, 0, defaultsize);
        }

        @Override
        public void getMessageListWithMore(int pattern, final ListFetchCallback<MessageMode> itemListFetchCallback) {
            MessageBusiness.getMessageList(new ApiProxy(new IProxyCallback() {
                @Override
                public boolean onProxyResult(ProxyResult<?> result) {
                    CtApiResponse response = (CtApiResponse) result.getData();
                    if (response != null && response.code == CtResponseCode.RESULT_OK) {
                        List<MessageMode> resultList = new ArrayList<>();
                        List<MessageServerItem> datas = (List<MessageServerItem>) response.result;
                        for (MessageServerItem item : datas) {
                            resultList.add(MessageMode.getInstance(item));
                        }
                        itemListFetchCallback.onSuc(resultList);
                        return true;
                    } else {
                        String msg;
                        if (response != null && !TextUtils.isEmpty(response.msg)) {
                            msg = response.msg;
                        } else {
                            msg = PlatformUtil.getInstance().getString(R.string.feedback_data_err);
                        }
                        itemListFetchCallback.onFailed(new CtException(msg));
                    }
                    return false;
                }
            }), userType, pattern, pattern + defaultsize);
        }

        @Override
        public void deleteMessage(String id, final CtApiCallback callback) {
            MessageBusiness.deleteMessage(new ApiProxy(new IProxyCallback() {
                @Override
                public boolean onProxyResult(ProxyResult<?> result) {
                    CtApiResponse response = (CtApiResponse) result.getData();
                    if (response != null && response.code == CtResponseCode.RESULT_OK) {
                        callback.onSuc();
                        return true;
                    } else {
                        String msg;
                        if (response != null && !TextUtils.isEmpty(response.msg)) {
                            msg = response.msg;
                        } else {
                            msg = PlatformUtil.getInstance().getString(R.string.feedback_data_err);
                        }
                        callback.onFailed(new CtException(msg));
                    }
                    return false;
                }
            }), id);
        }
    }
}
